<template>
  <div id="changedialog">
    <commonformdialog
      :isshowDialogs="isshowDialog"
      :dialogTitle="dialogTitle"
      @closetextDialog="closetextDialog"
      @confirmbtn="confirmbtn"
      :dialogWidth="'750px'"
    >
      <div class="innerLoding">
        <el-row>
          <el-form
            :model="formData"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            size="small"
          >
            <el-row>
              <el-col :span="20">
                <el-form-item label="标题：" prop="title">
                  <el-input
                    placeholder="请输入标题"
                    style="width: 80%"
                    v-model="formData.title"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="所在城市：" prop="city_id">
                  <el-cascader
                    ref="cascader"
                    filterable
                    placeholder="请选择所在城市"
                    v-model="formData.city_id"
                    :options="cityoptions"
                    :props="{
                      expandTrigger: 'hover',
                      
                      emitPath: false,
                    }"
                    clearable
                    :show-all-levels="false"
                    style="width: 80%"
                    @change="changeCity"
                  ></el-cascader>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="出发地：" prop="start_point">
                  <el-select
                    v-model="formData.start_point"
                    placeholder="请选择出发地"
                    style="width: 80%"
                    filterable
                    :disabled="!formData.city_id"
                  >
                    <el-option
                      :label="item.title"
                      :value="item.id"
                      v-for="(item, index) in cityoptionsc"
                      :key="index"
                    >
                     <span class="cityoptionscItem"><span>{{ item.title }} </span><span class="el-icon-camera-solid" v-if="item.isPhoto"></span></span>
                     
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="20">
                <el-form-item label="途经点：" prop="more_point">
                  <el-select
                    v-model="formData.more_point"
                    placeholder="请选择途经点"
                    style="width: 80%"
                    filterable
                    clearable
                    :disabled="!formData.city_id"
                  >
                    <el-option
                      :label="item.title"
                      :value="item.id"
                      v-for="(item, index) in cityoptionsc"
                      :key="index"
                    >
                       <span class="cityoptionscItem"><span>{{ item.title }} </span><span class="el-icon-camera-solid" v-if="item.isPhoto"></span></span>
                    </el-option>
                  </el-select>
                  <span
                    class="el-icon-circle-plus"
                    style="font-size: 20px; color: #1bbc9b; margin-left: 10px"
                    @click="addmorePoint"
                  ></span>
                </el-form-item>
              </el-col>
              <div v-for="(p, i) in more_pointArr" :key="i">
                <el-col :span="20">
                  <el-form-item prop="more_pointArrdata">
                    <el-select
                      v-model="more_pointArr[i].id"
                      placeholder="请选择途经点"
                      style="width: 80%"
                      filterable
                      clearable
                      :disabled="!formData.city_id"
                    >
                      <el-option
                        :label="item.title"
                        :value="item.id"
                        v-for="(item, index) in cityoptionsc"
                        :key="index"
                      >
                         <span class="cityoptionscItem"><span>{{ item.title }} </span><span class="el-icon-camera-solid" v-if="item.isPhoto"></span></span>
                      </el-option>
                    </el-select>
                    <span
                      class="el-icon-remove"
                      style="font-size: 20px; color: #ff4949; margin-left: 10px"
                      @click="removePoint(i)"
                    ></span>
                  </el-form-item>
                </el-col>
              </div>
              <el-col :span="20">
                <el-form-item label="目的地：" prop="end_point">
                  <el-select
                    v-model="formData.end_point"
                    placeholder="请选择目的地"
                    style="width: 80%"
                    filterable
                    :disabled="!formData.city_id"
                  >
                    <el-option
                      :label="item.title"
                      :value="item.id"
                      v-for="(item, index) in cityoptionMd"
                      :key="index"
                    >
                       <span class="cityoptionscItem"><span>{{ item.title }} </span><span class="el-icon-camera-solid" v-if="item.isPhoto"></span></span>
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="价格（元）：" prop="price">
                   <el-input-number   style="width: 80%"  placeholder="请输入价格" v-model="formData.price" :precision="2"></el-input-number>
  
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="备注：" prop="txt">
                  <el-input
                    type="textarea"
                    v-model="formData.txt"
                    :rows="3"
                    style="width: 80%"
                    resize="none"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-row>
      </div>
    </commonformdialog>
    <selecteMap
      :mapData="mapData"
      :showmap="isshowDialogMAP"
      :maptitle="dialogTitleMAP"
      @closetextDialogMap="closetextDialogMAP"
      @confirmbtnMap="confirmbtnMAP"
    ></selecteMap>
  </div>
</template>

<script type="text/javascript">
//引入弹窗
import commonformdialog from "@/components/commondialog/commondialog";

import selecteMap from "@/components/selecteMap";
import {
  prepareLineCreate,
  prepareLineUpdate,
  pointoption,
  hospitalOption
} from "@/api/dataPreparation";
export default {
  data() {
    return {
      showPicker: false,
      multipleopen: true,
      cityoptionsc: [],
      cityoptionMd: [],
      num: 0,
      more_pointArr: [],
      formData: {
        end_point: "",
        start_point: "",
        price: "",
        txt: "",
        city_id: "",
        more_point: "",
        title: "",
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        end_point: [
          { required: true, message: "请输入目的地", trigger: "blur" },
        ],
        start_point: [
          { required: true, message: "请输入目的地", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入价格", trigger: "blur" }],

        city_id: [{ required: true, message: "请选择城市", trigger: "blur" }],
      },
      isshowDialogMAP: false,
      dialogTitleMAP: "选择地址",
      mapData: {},
      selecte: true,
    };
  },
  props: {
    //是否显示弹窗
    isshowDialog: {
      type: Boolean,
      default: false,
      required: true,
    },
    //表单的名字
    dialogTitle: {
      type: String,
      default: "",
      required: true,
    },
    //是编辑还是新增
    formType: {
      type: Number,
      default: 0,
    },
    prepareLineOption: {
      type: Array,
    },
    cityoptions: {
      type: Array,
    },
    dialogdata: {
      type: Object,
    },
  },
  components: {
    commonformdialog,
    selecteMap,
  },
  methods: {
    addmorePoint() {
      if (!this.formData.city_id) {
        return;
      }
      this.more_pointArr.push({ id: "" });
    },
    changeCity() {
      this.formData.end_point = "";
      this.formData.start_point = "";
      this.formData.more_point = "";
      this.more_pointArr = [];
    },
    removePoint(i) {
      this.more_pointArr.splice(i, 1);
    },
    showMapSelecte() {
      this.isshowDialogMAP = true;
      if (this.formType == 1) {
        this.$nextTick(() => {
          let mapData = {};
          mapData.lat = this.formData.lat;
          mapData.lng = this.formData.lng;
          mapData.address = this.formData.address;
          this.mapData = mapData;
        });
      }
    },
    closetextDialogMAP() {
      this.isshowDialogMAP = false;
    },
    confirmbtnMAP(val) {
      if (val) {
        this.formData.lat = val.lat;
        this.formData.lng = val.lng;
        this.formData.address = val.address;
      }
      this.isshowDialogMAP = false;
    },
    closetextDialog() {
      this.$emit("closedialog");
    },
    //城市下拉
    async getpointoption(val) {
    let params
      if(val){
         params={ city_id: val }
      }
      let data = await pointoption(params);
      this.cityoptionsc = data.data;
    },
    //目的地下拉
    async gethospitalOption(val) {
    let params
      if(val){
         params={ city_id: val }
      }
      let data = await hospitalOption(params);
      this.cityoptionMd = data.data;
    },
    confirmbtn() {
      this.$refs["ruleForm"].validate(async (valid) => {
        if (!valid) return;

        let params = JSON.parse(JSON.stringify(this.formData));
        if (this.more_pointArr.length > 0) {
          this.more_pointArr.forEach((p) => {
            params.more_point += "," + p.id;
          });
        }
        let data;
        if (this.formType == 0) {
          data = await prepareLineCreate(params);
        } else {
          data = await prepareLineUpdate(params);
        }
        if (data.code == "0000") {
          this.$message({
            type: "success",
            message: data.msg,
          });
          this.$emit("confirm");
        }
      });
    },
  },
  watch: {
    isshowDialog: function (val) {
      if (!val) {
        
        this.$refs["ruleForm"].resetFields();
      }else{
        this.more_pointArr=[]
      }
    },
    dialogdata: function (val) {
      this.formData = val;
      let arr=[]
      if (this.formData.more_point && this.formData.more_point.length == 1) {
        arr = this.formData.more_point.split(",").map(Number);
        this.formData.more_point = arr[0];
        this.more_pointArr=[]
 
      } else if (this.formData.more_point.length > 1) {
        arr = this.formData.more_point.split(",").map(Number);
        this.formData.more_point = arr[0];
        arr.splice(0, 1);
        arr.forEach(p=>{
          this.more_pointArr.push({id:p})
        })
        console.log(this.formData.more_point,this.more_pointArr)
      }
    },
    "formData.city_id": function (val) {
        this.getpointoption(val);
        this.gethospitalOption(val);
    },
  },
  created() {},
};
</script>

<style lang="scss" scoped>
::v-deep {
  #InputSearch {
    display: none !important;
  }
  .grid-emojis {
    grid-template-columns: repeat(10, 10%) !important;
  }
  .small {
    position: absolute;
    z-index: 9;
    left: -45px;
    top: 30px;
  }
  .cityoptionscItem{
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
